<template>
    <el-dialog title="申请列表" :visible.sync="is_invite_list_show" :before-close="handleClose">
        <el-table @selection-change="handleSelectionChange" :data="data">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="项目名称" prop="projectName"></el-table-column>
            <el-table-column label="加入平台" prop="group"></el-table-column>
        </el-table>
        <div class="invite-group-submit">
            <el-button type="danger" @click="refuseAnApplication">拒绝</el-button>
            <el-button type="primary" @click="onSubmit">同意</el-button>
        </div>
    </el-dialog>
</template>

<script>
import group from '@/api/group.js';

export default {
    name: "invite-list",
    props: ["is_invite_list_show", 'data'],
    data() {
        return {
            multiple_selection: []
        };
    },
    methods: {
        handleClose() {
            this.multiple_selection = [];
            this.$emit("update:is_invite_list_show", false);
        },
        handleSelectionChange(val) {
            this.multiple_selection = val;
        },
        onSubmit() {
            let id = undefined;
            if(this.multiple_selection.length === 1) {
                id = this.multiple_selection[0].id
            } else if (this.multiple_selection.length > 1) {
                this.$message.error('只能单选!')
            } else if (this.multiple_selection <= 0) {
                return false;
            }
            group.agreeJoinGroup({id: id}).then(res => {
                this.$message({
                    type: 'success',
                    message: '添加成功!'
                });
                this.$store.dispatch('group/getApplyJoinGroupList');
            }).catch(err => {
                this.$message.error(err);
            });
            id = null;
            this.handleClose();
        },
        refuseAnApplication() {
            let id = undefined;
            if(this.multiple_selection.length === 1) {
                id = this.multiple_selection[0].id
            } else if (this.multiple_selection.length > 1) {
                this.$message.error('只能单选!')
            } else if (this.multiple_selection <= 0) {
                return false;
            }
            group.refuseAnApplication({id: id}).then(res => {
                this.$message({
                    type: 'success',
                    message: '拒绝成功!'
                });
                this.$store.dispatch('group/getApplyJoinGroupList');
            }).catch(err => {
                this.$message.error(err);
            });
            id = null;
            this.handleClose();
        }
    }
};
</script>

<style>
.invite-group-submit {
    margin-top: 15px;
    text-align: center;
}
</style>
